package com.nsl.my_youlai.Jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;

public class LeaderDeserializer extends StdDeserializer<Leader> {
    public LeaderDeserializer() {
        this(null);
    }

    protected LeaderDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public Leader deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String leaderName = p.getValueAsString();
        if (leaderName == null) {
            return null;
        }
        return getLeaderByName(leaderName);
    }

    private Leader getLeaderByName(String leaderName) {
        // TODO 查询数据库或通过其他操作返回leader详细信息
        if ("忽必烈".equals(leaderName)) {
            Leader leader = new Leader();
            leader.setLeaderName(leaderName);
            leader.setAge(79);
            leader.setNation("蒙古族");
            return leader;
        }
        return null;
    }
}

